﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperJoe.Framework.UnitOfWorkSql;
using WWX.Admin.IDAL;
using WWX.Dto.Search;
using WWX.Model;

namespace WWX.Admin.DAL
{
    public class ProductDAL : BaseDAL, IProductDAL
    {
        public ProductDAL(IUnitOfWork uow) : base(uow)
        {
        }

        public void Add(Product product)
        {
            var sql = "insert into products(id,no,name,price,remaincount,remark,isactive,isdeleted,createdby,createdtime,updatedby,updatedtime)values(?id,?no,?name,?price,?remaincount,?remark,?isactive,0,?createdby,?createdtime,?updatedby,?updatedtime)";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", product.Id);
            parameters.Add("no", product.No);
            parameters.Add("name", product.Name);
            parameters.Add("price", product.Price);
            parameters.Add("remaincount", product.RemainCount);
            parameters.Add("remark", product.Remark);
            parameters.Add("createdby", product.CreatedBy);
            parameters.Add("createdtime", product.CreatedTime);
            parameters.Add("updatedtime", product.UpdatedTime);
            parameters.Add("updatedby", product.UpdatedBy);
            parameters.Add("isactive", product.IsActive);

            this.Execute(sql, parameters);
        }

        public void Delete(string id)
        {
            var sql = "update productes set isdeleted=1 where id=?id";
            this.Execute(sql, new Dictionary<string, object>() { { "id", id } });
        }

        public List<Product> GetPageList(ProductAdminSearch search, int pageNo, int pageSize, out int total)
        {
            List<Product> retList = null;
            Dictionary<string, object> parameters = null;
            var whereSql = GenerateWhereSql(search, out parameters);

            string totalSql = "select count(1) from products where isdeleted=0 " + whereSql;
            total = this.GetCount(totalSql, parameters);
            if (total != 0)
            {
                var pagingSql = "select * from products where isdeleted=0 " + whereSql + " order by createdtime desc limit " + (pageNo - 1) * pageSize + "," + pageSize;

                retList = this.GetModels<Product>(pagingSql, parameters);

            }

            return retList ?? new List<Product>();
        }

        private string GenerateWhereSql(ProductAdminSearch search, out Dictionary<string, object> parameters)
        {
            parameters = this.CreateParameters();
            string sql = "";

            if (search.Name.IsNotNullOrWhiteSpace())
            {
                sql += " and name like ?name";
                parameters.Add("name", "%" + search.Name.Trim() + "%");
            }


            if (search.No.IsNotNullOrWhiteSpace())
            {
                sql += " and no like ?no";
                parameters.Add("no", "%" + search.No.Trim() + "%");
            }

            if (search.Remark.IsNotNullOrWhiteSpace())
            {
                sql += " and Remark like ?Remark";
                parameters.Add("Remark", "%" + search.Remark.Trim() + "%");
            }

            return sql;
        }

        public void Update(Product product)
        {
            var sql = "update products set no=?no,name=?name,price=?price,remaincount=?remaincount,remark=?remark,isactive=?isactive,updatedby=?updatedby,updatedtime=?updatedtime where id=?id";
            var parameters = new Dictionary<string, object>();
            parameters.Add("id", product.Id);
            parameters.Add("no", product.No);
            parameters.Add("name", product.Name);
            parameters.Add("price", product.Price);
            parameters.Add("remaincount", product.RemainCount);
            parameters.Add("remark", product.Remark);
            parameters.Add("updatedtime", product.UpdatedTime);
            parameters.Add("updatedby", product.UpdatedBy);
            parameters.Add("isactive", product.IsActive);

            this.Execute(sql, parameters);
        }

        public Product Get(string id)
        {
            var sql = "select * from products where id=?id";
            return this.GetModel<Product>(sql, new Dictionary<string, object>() { { "id", id } });
        }

        public Product GetByNo(string no)
        {
            var sql = "select * from products where no=?no and isdeleted=0";
            return this.GetModel<Product>(sql, new Dictionary<string, object>() { { "no", no } });
        }
    }
}
